//
//  RGMeARCodeVC.m
//  RedGrass
//
//  Created by wuyine on 2018/12/12.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGMeARCodeVC.h"
#import "DIYScanViewController.h"
#import "BaseNavigationViewController.h"
#import "YQQRCode.h"
#import "UIImage+Additions.h"
@interface RGMeARCodeVC ()
@property (nonatomic,strong) UIImageView *bgImageView;
@property (nonatomic,strong) UIImageView *contentImageView;
@property (nonatomic,strong) UIImageView *codeImage;
@property (nonatomic,strong) UIImageView *headIcon;
@property (nonatomic,strong) UIImage *screenShot;
@property (nonatomic,strong) UIView *saveToAlbumBtnView;
@property (nonatomic,strong) UIView *scanBtnView;
@property (nonatomic,strong) UILabel *userName;
@property (nonatomic,strong) UILabel *tipLabel;
@end

@implementation RGMeARCodeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.customNavBar.title = @"我的二维码";
    [self showBackButton];
    [self initUI];
    [self addMasonry];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.translucent = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = NO;
}

- (void)initUI {
    [self.customContentView addSubview:self.bgImageView];
    [self.customContentView addSubview:self.contentImageView];
    [self.contentImageView addSubview:self.codeImage];
    [self.codeImage addSubview:self.headIcon];
    [self.contentImageView addSubview:self.userName];
    [self.contentImageView addSubview:self.tipLabel];
    [self.customContentView addSubview:self.saveToAlbumBtnView];
    [self.customContentView addSubview:self.scanBtnView];
}

- (void)addMasonry {
    [_bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(0);
        make.top.mas_equalTo(-SafeAreaTopHeight);
    }];
    
    [_contentImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40);
        make.leading.mas_equalTo(40);
        make.trailing.mas_equalTo(-40);
        make.height.mas_equalTo((KScreenWidth-40*2)*400/315);
    }];
    
    [_codeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(80);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(172, 172));
    }];
    
    [_headIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(40,40));
        make.center.mas_equalTo(0);
    }];
    
    [_userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(_codeImage.mas_bottom).offset(35);
    }];
    
    [_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.equalTo(_userName.mas_bottom).offset(10);
    }];
    
    [_saveToAlbumBtnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentImageView.mas_bottom).offset(60);
        make.leading.equalTo(_contentImageView.mas_leading).offset(30);
    }];
    
    [_scanBtnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentImageView.mas_bottom).offset(60);
        make.trailing.equalTo(_contentImageView.mas_trailing).offset(-30);
    }];
}

- (void)saveToAlbum:(UIGestureRecognizer *)recog {
    UIImageWriteToSavedPhotosAlbum(self.screenShot, nil, nil,nil);
    [self showTextMessageWithMessage:@"保存成功"];
}

- (void)scan:(UIGestureRecognizer *)recog {
    DIYScanViewController *vc = [[DIYScanViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"二维码_bj")];
    }
    return _bgImageView;
}

- (UIImageView *)contentImageView {
    if (!_contentImageView) {
        _contentImageView = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"二维码_白框")];
    }
    return _contentImageView;
}

- (UIImageView *)codeImage {
    if (!_codeImage) {
        NSDictionary *param = @{@"userId":@([RGUserInfoModel getUserInfo].ID)};
        NSError *parseError = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:&parseError];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        _codeImage = [[UIImageView alloc] initWithImage:[YQQRCode enCodeWithString:jsonString withImageSize:172]];//WithImage:IMAGE_NAMED(@"二维码_头像")];
       
    }
    return _codeImage;
}

- (UILabel *)userName {
    if (!_userName) {
        _userName = [[UILabel alloc] init];
        _userName.text = [RGUserInfoModel getUserInfo].nickname;
        _userName.textColor = UICOLOR_WITH_RGBINT(0x000000);
        _userName.font = FONT(customFontName, 16);
    }
    return _userName;
}

- (UILabel *)tipLabel {
    if (!_tipLabel) {
        _tipLabel = [[UILabel alloc] init];
        _tipLabel.text = @"使用红草视界扫码，加我好友";
        _tipLabel.textColor = UICOLOR_WITH_RGBINT(0x999999);
        _tipLabel.font = FONT(customFontName, 12);
    }
    return _tipLabel;
}

- (UIImageView *)headIcon {
    if (!_headIcon) {
        _headIcon = [[UIImageView alloc] init];
        ViewBorderRadius(_headIcon, 5, 2, [UIColor whiteColor]);
        [_headIcon setImageURL:[NSURL URLWithString:[RGUserInfoModel getUserInfo].image]];
    }
    return _headIcon;
}

- (UIView *)saveToAlbumBtnView {
    if (!_saveToAlbumBtnView) {
        _saveToAlbumBtnView = [[UIView alloc] init];
        UIImageView *image = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"二维码_保存")];
        [_saveToAlbumBtnView addSubview:image];
        [image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(24, 24));
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(5);
        }];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"保存到相册";
        titleLabel.textColor = UICOLOR_WITH_RGBINT(0xffffff);
        titleLabel.font = FONT(customFontName, 12);
        [_saveToAlbumBtnView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.equalTo(image.mas_bottom).offset(16);
            make.bottom.mas_equalTo(0);
            make.leading.trailing.mas_equalTo(0);
        }];
        
        _saveToAlbumBtnView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(saveToAlbum:)];
        [_saveToAlbumBtnView addGestureRecognizer:tap];
    }
    return _saveToAlbumBtnView;
}

- (UIView *)scanBtnView {
    if (!_scanBtnView) {
        _scanBtnView = [[UIView alloc] init];
        UIImageView *image = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"二维码_扫一扫")];
        [_scanBtnView addSubview:image];
        [image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(24, 24));
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(5);
        }];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"扫一扫";
        titleLabel.textColor = UICOLOR_WITH_RGBINT(0xffffff);
        titleLabel.font = FONT(customFontName, 12);
        [_scanBtnView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.equalTo(image.mas_bottom).offset(16);
            make.bottom.mas_equalTo(0);
            make.leading.mas_equalTo(10);
            make.trailing.mas_equalTo(-10);
        }];
        
        _scanBtnView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scan:)];
        [_scanBtnView addGestureRecognizer:tap];
    }
    return _scanBtnView;
}

- (UIImage *)screenShot {
    if (!_screenShot) {
        _screenShot = [UIImage screenshot:self.codeImage limitWidth:1080];
    }
    return _screenShot;
}
@end
